package com.project.controller.web;

import com.project.entity.Department;
import com.project.entity.Major;
import com.project.myBeans.R;
import com.project.service.DepartmentService;
import com.project.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/major")
@Api(description = "专业接口",value = "major")
public class MajorController {
    @Autowired
    MajorService majorService;

    @Autowired
    DepartmentService departmentService;

    /**
     * wwh 2018/3/24
     * @param major
     * @return
     */
    @ApiOperation("添加专业")
    @PostMapping("/add")
    public R add(@RequestBody Major major){
        try{
            if(majorService.findByCondition(major).size()!=0){
                return R.error("该专业或类似名称专业已存在");
            }else {
                Department department = departmentService.findById(major.getDepartmentid());
                major.setSchoolid(department.getSchoolid());
                major.setUniversityid(department.getUniversityid());
                majorService.add(major);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/5/23
     *
     * @param depid
     * @return
     */
    @ApiOperation("根据系部id查找专业")
    @PostMapping("/findmajorbydepid/{depid}")
    public R findmajorbydepid(@PathVariable Integer depid) {
        try {
            List<Major> list = majorService.findByDepartmentid(depid);
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
